#include <stdio.h>

#include <unistd.h>

#include "ohos_init.h"
#include "cmsis_os2.h"

#include "hi_io.h"
#include "hi_adc.h"
#include "hi_gpio.h"

#define DELAY_10MS 10*1000
#define DELAY_100MS 100*1000
#define DELAY_1S 1*1000*1000

#define PHO_RES HI_GPIO_IDX_9

static void *PHO_RES_Task(const char *arg)
{
    (void)arg;

    uint16_t adc_value = 0;
    float adc_voltage = 0;

    /** GPIO Initialization **/
    hi_gpio_init();
    
    /** Set pin 9 function to GPIO **/
    hi_io_set_func(PHO_RES, HI_IO_FUNC_GPIO_9_GPIO);
    /** Set GPIO09 direction to output **/
    hi_gpio_set_dir(PHO_RES, HI_GPIO_DIR_IN);
    while(1)
    {
        hi_adc_read(HI_ADC_CHANNEL_4, &adc_value, HI_ADC_CUR_BAIS_DEFAULT, HI_ADC_EQU_MODEL_8, 0);
        printf("ADC_VALUE:%d\r\n", adc_value);
        adc_voltage = hi_adc_convert_to_voltage(adc_value);
        printf("adc_voltage:%f\r\n", adc_voltage);
        usleep(DELAY_100MS);
    }

    return NULL;
}

static void PHO_RES_TaskEntry(void)
{
    osThreadAttr_t attr;

    attr.name = "PHO_RES_Task";     
    attr.attr_bits = 0U;            
    attr.cb_mem = NULL;             
    attr.cb_size = 0U;              
    attr.stack_mem = NULL;          
    attr.stack_size = 4096;         
    attr.priority = 11;             

    if (osThreadNew((osThreadFunc_t)PHO_RES_Task, NULL, &attr) == NULL) {
        printf("[PHO_RES_TaskEntry] Failed to create PHO_RES_Task!\n");
    }
}

SYS_RUN(PHO_RES_TaskEntry);
